# -*- coding: UTF-8 -*-
# *******************************************************************
# File Name: u.py
# > Author: 04000387
# > Created Time: 2024/12/27 14:08
# *******************************************************************
from pathlib import Path
import json


def load_img(_base_path):
    _base_path = Path(_base_path)
    _img_folder = "train_full_images_"
    _children = ["0", "1"]
    _img_dict = dict()
    for _it in _children:
        _img_folder_item = _base_path / (_img_folder + _it)

        for _img_path in _img_folder_item.iterdir():
            _name = _img_path.stem
            _img_dict[_name] = _img_path

    return _img_dict


img_dict = load_img("E:\\01_git\\generate_dir\\test")

path = "E:\\01_git\\generate_dir\\test\\train_full_labels.json"

fp = open(path, "r", encoding="utf-8")

res = json.load(fp)

jsonl_path = "./real_world.jsonl"

fp1 = open(jsonl_path, "w")

item_list = []
for img_stem in res:
    if img_stem not in img_dict:
        continue

    img_path = img_dict[img_stem].absolute()

    patch_list = []
    for patch in res[img_stem]:
        patch_text = patch["transcription"]
        if patch_text == "###":
            continue

        patch_list.append(patch_text)

    string = "这是一张带有文字的图片,图片中的文字如下:" + "\n".join(patch_list)
    one = json.dumps({"img_file": str(img_path), "content": string}) + "\n"

    item_list.append(one)

fp1.writelines(item_list)

fp1.close()

fp.close()
